package com.mk.common.util;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.junit.Test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mk.common.model.BizParamDTO;

import io.jsonwebtoken.lang.Collections;

public class MapDemo
{
	static Map<String,Integer> oneMap = new HashMap<>();
	static {
		oneMap.put("b", 1);
		oneMap.put("c", 3);
		oneMap.put("a", 2);
		oneMap.put("s", 1);
		oneMap.put("e", 18);
	}
	
	@Test
	public void testInit() {
		// 初始化 
		Map<Integer, List<Integer>> quarterMap = new HashMap<Integer, List<Integer>>() {{
			put(1, Arrays.asList());
		}};
	}
	
	@Test
	public void testLoop() {
		oneMap.forEach((k, v)->{
			oneMap.put(k, v*10);
		});
		
		System.out.println(oneMap);
	}
	
	
	// ##############
	static Map<?, Function<?,?>> actionMap = new HashMap<>();
	
	@Test
	public void testJson() {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("busReqId", oneMap.get("e"));
//		System.out.println( oneMap.get("e") );
		
		Integer reqId = jsonObject.getInteger("busReqId");
		
		System.out.println(reqId);
	}
	
	/**
	 * Map迭代方式
	 */
	@Test
	public void test1() {
		/***
		 * 1: 在for-each循环中使用entries来遍历
		 * 	只适用于java5及以上版本
		 */
//		for(Map.Entry<String, Integer> entry: oneMap.entrySet()) {
//			System.out.println("key: "+entry.getKey()+", value: "+entry.getValue());
//		}
		
		/**
		 * 2: 在for-each循环中遍历keys或values
		 */
//		for (String key : oneMap.keySet()) {
//		    System.out.println("Key = " + key);
//		}
		//GOMZ_DATA_HAB01_BAK， GOMZ_DATA_HAB01 AHAF0001 GOMZ_DATA_HZB02_BAK GOMZ_DATA_HZB02
//		for (Integer value : oneMap.values()) {
//		    System.out.println("Value = " + value);
//		}
		
		/**
		 * 3: 使用Iterator遍历
		 * 	支持低版本; 可以在遍历时调用iterator.remove()来删除entries
		 */
		Iterator<Map.Entry<String, Integer>> entries = oneMap.entrySet().iterator();
		while (entries.hasNext()) {
		    Map.Entry<String, Integer> entry = entries.next();
			if("a".equals(entry.getKey()) && entry.getValue()==2 ) {
		    	entry.setValue(2300);
		    }
		    System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
		}
		
		
		/**
		 * 4: 1.8 新增
		 */
//		oneMap.forEach((key, value)->{
//			System.out.println("key="+key+", value="+value);
//		});
		
		
		
	}
	
	
	@Test
	public void test2() {
		BizParamDTO bizParamDTO = new BizParamDTO();
		bizParamDTO.put("eventIs", "223588282adc4a7187b421d7082b9514");
		bizParamDTO.setBizType("b004");
		
		//BizParamDTO [eventId=null, bizType=b004, operateType=null, eventStatus=null, userId=null]
		System.out.println(bizParamDTO);
		//{"eventIs":"223588282adc4a7187b421d7082b9514"}
		System.out.println(JSON.toJSONString(bizParamDTO));
	}
	
	
	@Test
	public void test3() {
		Map<String,String> param = new HashMap<>();
		param.put("KSDM", "1");
		param.put("KSSJ", "2021-01-01");
		
		Map<String, String> userMap = new HashMap<>(2);
		userMap.put("hisOrganizationCode", "");
		
		param.putAll(userMap);
		
		System.out.println( JSON.toJSONString(param) );
	}
	
	@Test
	public void test4() {
		//hashMap 中指定 key 的值进行重新计算，如果不存在这个 key，则添加到 hashMap 中;
		// 若存在key, key值 不变
		oneMap.computeIfAbsent("a", val->200);
		oneMap.computeIfAbsent("d", v->1);
		System.out.println( oneMap );// {a=2, b=1, c=3, s=1, d=1, e=18}
	}
	
	@Test
	public void test5() {
		oneMap.merge("a", 200, ((oldVal, newVal)->700) );
		System.out.println(oneMap); // {a=700, b=1, c=3, s=1, e=18}
		
		oneMap.merge("SD", 600, (oldVal, newVal)->newVal );
		System.out.println(oneMap); // {SD=600, a=200, b=1, c=3, s=1, e=18}
		
		oneMap.merge("ATM", 350, (old,newVal)->old+newVal);
		System.out.println(oneMap); // {SD=600, a=700, b=1, c=3, s=1, e=18, ATM=350}
	}
	
	@Test
	public void test6() {
		String temp = "审批意见";
		A: try {
			if("审批意见".equals(temp)) {
				break A;
			}
			System.out.println("比较计算....");
		} catch(Exception e ) {
			e.printStackTrace();
		} 
		System.out.println("结束。。。");
	}
	
	@Test
	public void test7() {
//		int a  = (23);
		Map<String, Object> map = new HashMap<>(1);
		map.putAll(null);
	}
	
	@Test
	public void test8() {
		Map<String, Object> param = new HashMap<>();
		
		param.put("ztlag", "false");
		param.put("lwtag", "true");
		param.put("resign", "false");
		
		param.forEach( (key, value)->{
			if( "true".equals(String.valueOf(value)) 
					|| "false".equals(String.valueOf(value)) ) {
				param.put(key, Boolean.valueOf(String.valueOf(value)));
			}
		});
		
		//打印
		System.out.println(param);
	}
	
	@Test
	public void test9() {
		if(oneMap.get("123")==null) {
			Integer item = oneMap.get("123");
			String s = String.valueOf(item);
			System.out.println("null".equals(s));
		}
	}
	
	
	@Test
	public void test10() {
		Map map = new HashMap<>();
		Object k = map.get("");
		System.out.println( Boolean.valueOf( String.valueOf(k) ) );
	}
	
	@Test
	public void test11() {
		Map<Integer, String> map = new HashMap<>();
//		map.put(null, 1); //编译不通过 ×××
	}
	
	/**
	 * Map中的键值对分别转为数组, toArray()方式
	 */
	@Test
	public void test12() {
		String[] srr = oneMap.keySet().toArray(new String[0]);
		Integer[] iarr = oneMap.values().toArray(new Integer[0]);
		System.out.println(Arrays.toString(srr));
		System.out.println(Arrays.toString(iarr));
	}
	
	/**
	 * Map中的键值对分别转为数组, stream()方式
	 */
	@Test
	public void test13() {
		String[] srr = oneMap.keySet().stream().toArray(String[]::new);
		Integer[] iarr = oneMap.values().stream().toArray(Integer[]::new);
		System.out.println(Arrays.toString(srr));
		System.out.println(Arrays.toString(iarr));
	}
	
	@Test
	public void test14() {
		Map<String,String> tmap = new HashMap<>();
		tmap.put("ds", "111111");
		
		tmap.computeIfAbsent("ds", v->"323");
		tmap.computeIfAbsent("w", val->val);
		
		System.out.println(tmap); // {w=w, ds=111111}
	}
	
	@Test
	public void test15() {
		Map<String, Object> tmap = new HashMap<>();
		tmap.put("ds", "111111");
//		tmap.put("content", null);
		tmap.put("content", "");
		
//		System.out.println(tmap.get("content")); // null
//		String val = Optional.ofNullable( tmap.get("content") )
//			.filter(t->t!=null)
//			.map(u->String.valueOf(u)).orElseGet(()->"无");
//		System.out.println(val);
		
		Optional.ofNullable(tmap.get("content")).ifPresent(u->{
			String[] contents = String.valueOf(u).split("\n");
			List<String> list = Stream.of(contents).filter(t->!StringUtils.isEmpty(t)).collect(Collectors.toList());
//			System.out.println();
			System.out.println(JSON.toJSONString(list));
		});
	}
	
	@Test
	public void test16() {
		Map<String, Object> tmap = new HashMap<>();
		tmap.put("ds", "111111");
//		tmap.put("content", null);
		tmap.put("content", "");
		
		tmap.computeIfAbsent("content", val -> "");
		
		System.out.println(tmap);
	}
	
	@Test
	public void test17() {
		Map<String, Object> param = new HashMap<>();
		param.put("KHLX", "175");
		param.put("JCJB", 3);
		param.put("KSRQ", "2021-09-11");
		param.put("JSRQ", "2021-10-01");
		
		System.out.println( param.containsKey(null) );
	}
	
	
}
